package net.refractions.postgis;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.postgis.Geometry;
import org.postgis.LinearRing;
import org.postgis.PGgeometry;
import org.postgis.Point;
import org.postgis.Polygon;

/**
 * 
 * http://postgis.refractions.net/documentation/manual-1.5/ch05.html
 * 
 * @author nathanleewei
 * 
 */
public class PostGisJava {

	public static void main(String[] args) {

		java.sql.Connection conn;

		try {
			/*
			 * Load the JDBC driver and establish a connection.
			 */
			Class.forName("org.postgresql.Driver");
			String url = "jdbc:postgresql://localhost:5432/database";
			conn = DriverManager.getConnection(url, "postgres", "");
			/*
			 * Add the geometry types to the connection. Note that you must cast the connection to the pgsql-specific
			 * connection implementation before calling the addDataType() method.
			 */
			((org.postgresql.PGConnection) conn).addDataType("geometry", Class.forName("org.postgis.PGgeometry"));
			((org.postgresql.PGConnection) conn).addDataType("box3d", Class.forName("org.postgis.PGbox3d"));

			/*
			 * Create a statement and execute a select query.
			 */
			Statement s = conn.createStatement();
			ResultSet r = s.executeQuery("select geom,id from geomtable");
			while (r.next()) {
				/*
				 * Retrieve the geometry as an object then cast it to the geometry type. Print things out.
				 */
				PGgeometry geom = (PGgeometry) r.getObject(1);
				int id = r.getInt(2);
				System.out.println("Row " + id + ":");
				System.out.println(geom.toString());
			}
			s.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void geometry(ResultSet r) throws SQLException {
		PGgeometry geom = (PGgeometry) r.getObject(1);
		if (geom.getGeoType() == Geometry.POLYGON) {
			Polygon pl = (Polygon) geom.getGeometry();
			for (int i = 0; i < pl.numRings(); i++) {
				LinearRing rng = pl.getRing(i);
				System.out.println("Ring: " + r);
				for (int p = 0; p < rng.numPoints(); p++) {
					Point pt = rng.getPoint(p);
					System.out.println("Point: " + p);
					System.out.println(pt.toString());
				}
			}
		}
	}
}
